#pragma once

#include "World/Z2GProtocol.h"

struct XPlayerAgent;

class XZoneAgent
{
public:
    XZoneAgent(void);
    ~XZoneAgent(void);

    BOOL Setup(ISocketMgr* piSocketMgr);
    void Clear();

    void Activate();

private:
    void Send(void* pvData, size_t uDataLen);

    static void ErrorCallbackFunc(XSocket* pSocket, void* pvUsrData, int nSysErr, int nUsrErr);
    static void DataCallbackFunc(XSocket* pSocket, void* pvUsrData, BYTE* pbyData, size_t uDataLen);

    void ProcessSocketError(XSocket* pSocket, void* pvUsrData, int nSysErr, int nUsrErr);
    void ProcessReceivePackage(XSocket* pSocket, void* pvUsrData, BYTE* pbyData, size_t uDataLen);

private:
    typedef void (XZoneAgent::*XZ2G_PROTOCOL_FUNC)(BYTE* pbyData, size_t uDataLen);

    XZ2G_PROTOCOL_FUNC m_PakProcessor[z2g_protocol_total];
    size_t             m_uPakSize[z2g_protocol_total];

    ISocketMgr*        m_piSocketMgr;
    XSocket*           m_pSocket;
    DWORD              m_dwNextPingTime;

public:
    void DoPingSignal();
    void DoPlayerLoginRequest(XPlayerAgent* pPlayer);

private:
    void OnPlayerLoginRespond(BYTE* pbyData, size_t uDataLen);
    void OnSyncLoginInfo(BYTE* pbyData, size_t uDataLen);

};

extern XZoneAgent* g_pZoneAgent;
